//
//  HYMovieMoreViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "HYMovieMoreViewController.h"
#import "HYSalonViewController.h"
#import "HYShadowViewController.h"
#import "HYShortViewController.h"
#import "HYShowViewController.h"
#import "HYOriginalViewController.h"
#import "HYMovieMoreCollectionViewCell.h"
#import "PKRequestManager.h"
#import "CJShadowMovieItemdataModel.h"
#import "CJShadowListTableViewCell.h"
#import <UIButton+WebCache.h>
#import "UMSocial.h"

@interface HYMovieMoreViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) HYSalonViewController *salonVC;
@property (nonatomic, strong) HYShadowViewController *shadowVC;
@property (nonatomic, strong) HYShortViewController *shortVC;
@property (nonatomic, strong) HYShowViewController *showVC;
@property (nonatomic, strong) HYOriginalViewController *originalVC;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CJShadowMovieItemdataModel *model;


@end

static NSString *identifier = @"HYMovieMoreCollectionViewCell";
static NSString *ident = @"CJShadowListTableViewCell";
@implementation HYMovieMoreViewController


- (void) setUpData {
    [PKRequestManager requestWithUrlString:@"http://morguo.com/forum.php?mod=movie&type=2&androidflag=1&appfrom=ios&iosversion=3.13&page=1" parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        NSArray *listArray = dataDic[@"data"][@"list"];
        for (NSDictionary *dic in listArray) {
            _model = [[CJShadowMovieItemdataModel alloc] init];
            [_model setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:_model];
        }
        [self addTableView];
        
    } error:^(NSError *error) {
        
    }];

}


- (void)viewDidLoad {
    _dataArray = [NSMutableArray array];
    
    [super viewDidLoad];
    
    [self setUpData];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButton)];
    self.navigationItem.leftBarButtonItem = leftButton;
    [self creatChildViewController];
    _titleArray = @[@"短片",@"影单",@"沙龙",@"电影原声"];
    

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 设置集合视图为横向滚动
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 5;
    
    // 创建集合视图
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    // 隐藏掉横向滚动条
    collectionView.showsHorizontalScrollIndicator = NO;
    // 注册自定义cell类
    [collectionView registerClass:[HYMovieMoreCollectionViewCell class] forCellWithReuseIdentifier:identifier];
    
    [self.view addSubview:collectionView];
    
        
}

- (void)addTableView {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, kScreenWidth, kScreenHeight- 143) style:(UITableViewStylePlain)];
    [_tableView registerClass:[CJShadowListTableViewCell class] forCellReuseIdentifier:ident];
  
    _tableView.rowHeight = 400;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}
#pragma mark --添加子式图控制器--
- (void)creatChildViewController {
    //上映
    _showVC = [[HYShowViewController alloc] init];
    [self addChildViewController:_showVC];
    [self.view addSubview:_showVC.view];
    
    
}

- (void)leftButton {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark--返回tableview行数--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CJShadowListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
    CJShadowMovieItemdataModel *model = _dataArray[indexPath.row];
    cell.nameLabel.text = model.subject;
    cell.timeLabel.text = model.moviedata.pubdate;
    cell.directorsLabel.text = model.moviedata.directors;
    cell.castsLabel.text = model.moviedata.casts;
    NSString *string = [NSString stringWithFormat:@"http://morguo.com/%@",model.moviedata.image];
    [cell.imageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:string] forState:(UIControlStateNormal)];
    cell.messageLabel.text = model.message;
    
    cell.bottomView.praiseButton.tag = 2000 + indexPath.row;
    [cell.bottomView.praiseButton addTarget:self action:@selector(praiseButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    cell.bottomView.moreButton.tag = 20500 + indexPath.row;
    [cell.bottomView.moreButton addTarget:self action:@selector(moreButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    return cell;
    
}

#pragma mark --- 返回分区CollectionView ---------------------------------------------------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark --- 返回item个数 ---
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _titleArray.count;
}

#pragma mark --- 返回cell ---
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HYMovieMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.title.text = _titleArray[indexPath.row];
       return cell;
}

#pragma mark --- 返回item大小 ---
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kScreenWidth /4, 30);
}

//点赞
- (void)praiseButton:(UIButton *)button {
    
    
    // 是否登录userName isEqualToString:@"YES"]
    if ([SZLoginTool shareLogin].isLogin) {
        
        button.selected = !button.selected;
        
        NSString *isLike = nil;
        if (button.selected) {
            isLike = @"like";
        } else {
            isLike = @"unlike";
        }
        
        NSString *str = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=threadlikecp&op=%@&tid=%@&formhash=%@&androidflag=1&appfrom=ios&iosversion=3.13",isLike,_model.tid, [SZLoginTool shareLogin].formhash];
        
        [PKRequestManager requestWithUrlString:str parDic:nil method:GET finish:^(NSDictionary *dataDic) {
            if (dataDic[@"data"][@"result"]) {
                SZLog(@"点赞成功");
                
                _model.isliked = button.selected;
            } else {
                SZLog(@"点赞失败");
            }
            
            
        } error:^(NSError *error) {
            
        }];
        
    } else {
        
        SZLoginViewController *loginVC = [[SZLoginViewController alloc] init];
        UINavigationController *loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNav animated:YES completion:nil];
        
        
    }
}

//分享
- (void)moreButton:(UIButton *)button {
    
    NSString *url = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=viewthread&tid=%@&isfrommobile=1",_model.tid];
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *viewController = window.rootViewController;
    [UMSocialSnsService presentSnsIconSheetView:viewController
                                         appKey:@"569ee21fe0f55a76b10021e3"
                                      shareText:[NSString stringWithFormat:@"<<%@>>详情点击%@",_model.subject,url]
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToEmail,UMShareToWechatFavorite,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToTencent,nil]
                                       delegate:nil];
    
    
}


#pragma mark --点击item触发方法--
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0:
            //    短片
            _shortVC = [[HYShortViewController alloc] init];

           [self.navigationController pushViewController:_shortVC animated:YES];
            break;
            
            case 1:
            //影单
            _shadowVC = [[HYShadowViewController alloc] init];

            [self.navigationController pushViewController:_shadowVC animated:YES];
            break;
            
            case 2:
            //沙龙
            _salonVC = [[HYSalonViewController alloc] init];

            [self.navigationController pushViewController:_salonVC animated:YES];
            break;
            
            case 3:
            
            //电影原声
            _originalVC = [[HYOriginalViewController alloc] init];
            [self.navigationController pushViewController:_originalVC animated:YES];
            break;
            
            default:
            break;
    }
  
}

@end
